package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.constant.CacheNames;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.EmgUserBo;
import com.ruoyi.system.domain.vo.EmgUserVo;
import com.ruoyi.system.domain.EmgUser;
import com.ruoyi.system.mapper.EmgUserMapper;
import com.ruoyi.system.service.IEmgUserService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 救援人员Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-10
 */
@RequiredArgsConstructor
@Service
public class EmgUserServiceImpl implements IEmgUserService {

    private final EmgUserMapper baseMapper;

    /**
     * 查询救援人员
     */
    @Override
    public EmgUserVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询救援人员列表
     */
    @Override
    public TableDataInfo<EmgUserVo> queryPageList(EmgUserBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EmgUser> lqw = buildQueryWrapper(bo);
        Page<EmgUserVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询救援人员列表
     */
    @Override
    public List<EmgUserVo> queryList(EmgUserBo bo) {
        LambdaQueryWrapper<EmgUser> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EmgUser> buildQueryWrapper(EmgUserBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EmgUser> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTeamId() != null, EmgUser::getTeamId, bo.getTeamId());
        lqw.eq(StringUtils.isNotBlank(bo.getUserTypeId()), EmgUser::getUserTypeId, bo.getUserTypeId());
        lqw.eq(StringUtils.isNotBlank(bo.getAccount()), EmgUser::getAccount, bo.getAccount());
        lqw.eq(StringUtils.isNotBlank(bo.getPassword()), EmgUser::getPassword, bo.getPassword());
        lqw.like(StringUtils.isNotBlank(bo.getName()), EmgUser::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getAvatarUrl()), EmgUser::getAvatarUrl, bo.getAvatarUrl());
        lqw.eq(bo.getAge() != null, EmgUser::getAge, bo.getAge());
        lqw.eq(StringUtils.isNotBlank(bo.getSex()), EmgUser::getSex, bo.getSex());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), EmgUser::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getMajorId()), EmgUser::getMajorId, bo.getMajorId());
        lqw.eq(StringUtils.isNotBlank(bo.getLat()), EmgUser::getLat, bo.getLat());
        lqw.eq(StringUtils.isNotBlank(bo.getLng()), EmgUser::getLng, bo.getLng());
        lqw.eq(StringUtils.isNotBlank(bo.getAreaId()), EmgUser::getAreaId, bo.getAreaId());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), EmgUser::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getGoodAt()), EmgUser::getGoodAt, bo.getGoodAt());
        lqw.eq(bo.getCombatCount() != null, EmgUser::getCombatCount, bo.getCombatCount());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), EmgUser::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增救援人员
     */
    @Override
    public Boolean insertByBo(EmgUserBo bo) {
        EmgUser add = BeanUtil.toBean(bo, EmgUser.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改救援人员
     */
    @Override
    public Boolean updateByBo(EmgUserBo bo) {
        EmgUser update = BeanUtil.toBean(bo, EmgUser.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EmgUser entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除救援人员
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
